package com.emm.yixun.website.controller.marketing;
//框架包
import java.util.*;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


//common
import com.emm.yixun.common.page.PageUtil;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.common.model.User;
import com.emm.yixun.system.service.UserServiceFacade;
//website
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.base.Const;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;
//变化的引用
import com.emm.yixun.common.model.SalesGroupUser;
import com.emm.yixun.common.model.dto.RootClass;
import com.emm.yixun.common.model.dto.SalesGroupDto;
import com.emm.yixun.common.model.dto.SalesGroupUserDto;
import com.emm.yixun.common.model.dto.UserDto;
import com.emm.yixun.marketing.service.SalesGroupUserServiceFacade;
@Controller
@RequestMapping("/marketing/salesGroupUser/")
public class SalesGroupUserController extends BaseController<SalesGroupUser>{
	
	@Autowired
	private SalesGroupUserServiceFacade salesGroupUserServiceFacade;
	@Autowired
	private UserServiceFacade userServiceFacade;
	
	@RequestMapping(value="index")
	public String index(ModelMap map){
		if(doSecurityIntercept(Const.RESOURCES_TYPE_MENU)){
			//查询菜单按钮权限
			map.put("permitBtn", getPermitBtn(Const.RESOURCES_TYPE_FUNCTION));		
			return "/marketing/salesGroupUser/list";
		}
		return Const.NO_AUTHORIZED_URL;
	}
	
	//新增返回实体对象
	@RequestMapping(value="add", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  add(SalesGroupDto dto){
    	AjaxRes ar=getAjaxRes();
    	
    	/*
    	 *1.判断权限 
    	 *2.调用新增接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"marketing:salesGroup:add"))){
        	try {
        		User user = ShiroUtil.getCurrentUser();
        		JSONArray jsonarray = JSONArray.fromObject(dto.getBatchJson());
        		@SuppressWarnings("unchecked")
				List<Object> objectList = (List<Object>) JSONArray.toCollection(jsonarray, Object.class);
        		List<SalesGroupUserDto> salesList = new ArrayList<SalesGroupUserDto>();
        		for (int i = 0; i < objectList.size(); i++) {
	        		JSONObject jsonObject=JSONObject.fromObject(objectList.get(i));
	        		SalesGroupUserDto obj = (SalesGroupUserDto)JSONObject.toBean(jsonObject, SalesGroupUserDto.class);
	        		salesList.add(obj);	
	        	}
				Response<Integer> response=salesGroupUserServiceFacade.batchGroupUser(user, dto, salesList);
				if(response.isSuccess()){
					ar.setSucceedMsg(Const.SAVE_SUCCEED);
				}else{
					ar.setFailMsg(response.getResultMessage());
					ar.setFailMsg(response.getResultMessage());
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.SAVE_FAIL);
			}
    	}
    	return ar;
    }
	//查看
	@RequestMapping(value="view", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  view(Long id){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用查看接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"marketing:salesGroupUser:view"))){
        	try {
				Response<SalesGroupUser> response=salesGroupUserServiceFacade.findById(id);
				if(response.isSuccess()){
					ar.setSucceed(response.getEntity());
				}else {
					ar.setFailMsg(Const.DATA_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
    	}
    	return ar;
    }
	
	//修改
	@RequestMapping(value="edit", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  edit(SalesGroupUserDto dto){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用修改接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"marketing:salesGroupUser:edit"))){
        	try {
        		Response<Integer> response=salesGroupUserServiceFacade.updateByPrimaryKey(dto, true);
				if(response.isSuccess()){
					ar.setSucceedMsg(Const.UPDATE_SUCCEED);
				}else{
					ar.setFailMsg(Const.UPDATE_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.UPDATE_FAIL);
			}
    	}
    	return ar;
    }
	
	//删除
	@RequestMapping(value="delete", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  delete(Long id){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用查看接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"marketing:salesGroupUser:delete"))){
        	try {
        		Response<Integer> response=salesGroupUserServiceFacade.deleteByPrimaryKey(id);
				if(response.isSuccess()){
					ar.setSucceedMsg(Const.DEL_SUCCEED);
				}else {
					ar.setFailMsg(Const.DEL_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.DEL_FAIL);
			}
    	}
    	return ar;
    }
	
	
	
	//分页查询
	@RequestMapping(value="list")
	@ResponseBody
    public Page<SalesGroupUserDto>  list(SalesGroupUserDto dto,Integer page,Integer limit){
    	/*
    	 *1.判断权限 
    	 *2.调用分页查询接口
    	 *3.返回信息
    	*/
		if(dto.getSalesGroupId()!=null){
			 if(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"marketing:salesGroup:edit")){
				 User user = ShiroUtil.getCurrentUser();
				 dto.setMerchantId(user.getMerchantId());
				 try {
					 Page<SalesGroupUserDto> response=salesGroupUserServiceFacade.findSalesGroupUserDto(dto, PageUtil.begin(page, limit),limit);
	                if(response.getSuccess()){ 
	                if(null==response.getRows())response.setRows(new ArrayList<SalesGroupUserDto>());
	            	response.setPageNumber(limit);
	            	return response;
	                }
				 } catch (Exception e) {
					logger.error(e.toString(),e);
				 }
	    	}
		}
    	return new Page<SalesGroupUserDto>();
    }
		
	@RequestMapping(value="findSalesGroupUserDto")
	@ResponseBody
    public Page<SalesGroupUserDto>  findSalesGroupUserDto(SalesGroupUserDto dto,Integer page,Integer limit){
		if(dto.getSalesGroupId()!=null){
			if(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"marketing:salesGroup:view")){
				try {
	        		Page<SalesGroupUserDto> response=salesGroupUserServiceFacade.findSalesGroupUserDto(dto, PageUtil.begin(page, limit),limit);
	                if(response.getSuccess()){ 
	                if(null==response.getRows())response.setRows(new ArrayList<SalesGroupUserDto>());
	            	response.setPageNumber(limit);
	            	return response;
	                }
				} catch (Exception e) {
					logger.error(e.toString(),e);
				}
	    	}
		}
    	return new Page<SalesGroupUserDto>();
    }
	
	@RequestMapping(value = "findSystemUserList")
	public @ResponseBody RootClass findSystemUserList(Boolean isDataPermission){
		if(null==isDataPermission) isDataPermission=false;
		User user = ShiroUtil.getCurrentUser();
		UserDto dto = new UserDto();
		dto.setIsAdmin("0");
		dto.setDelFlag("0");
		dto.setUserStatus("0");
		dto.setMerchantId(ShiroUtil.getCurrentUser().getMerchantId());
		dto.setResourcesCode("menu_0002_0001");
		dto.setPermissionUserId(user.getId());
		dto.setColumnName("user_department_path");
		RootClass root = new RootClass();
		ResponseList<User> userList = userServiceFacade.findDataPermissionByDto(dto,isDataPermission);
		root.setRoot(userList.getEntity());
		return root;
	}
}
